package com.somnus.androidkotlindemo.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.util.DiffUtil
import android.support.v7.widget.LinearLayoutManager
import com.somnus.androidkotlindemo.R
import com.somnus.androidkotlindemo.adapter.DiffUtilsAdapter
import com.somnus.androidkotlindemo.callback.DiffUtilsCallback
import kotlinx.android.synthetic.main.activity_recycler_view.*


class DiffUtilsActivity : AppCompatActivity() {

    lateinit var list: MutableList<String>
    lateinit var newList: MutableList<String>
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler_view)

        initNewData()
        initData()
        val adapter = DiffUtilsAdapter()
//        adapter.setData(list)
        adapter.setNewData(list)
        recycler.layoutManager = LinearLayoutManager(this)
        recycler.adapter = adapter

        adapter.setOnItemClickListener { _adapter, view, position ->

            newList = ArrayList()
            newList.addAll(list)

//            newList = _adapter.data as MutableList<String>
            newList.set(position, "new list$position")


//            val diffResult = DiffUtil.calculateDiff(DiffUtilsCallback(_adapter.data as List<String>, newList), true)
//
//            diffResult.dispatchUpdatesTo(adapter)
//
            adapter.setDiffList(newList)





//            adapter.DiffData(newList)

        }
    }

    //创建一个 伴生对象
    companion object {
        fun startDiffUtilsActivity(context: Context) {

            val mIntent = Intent()
            mIntent.setClass(context, DiffUtilsActivity::class.java)
            context.startActivity(mIntent)
        }
    }


    private fun initData(): List<String> {
        list = ArrayList<String>()
        for (i in 0..50) {
            (list as ArrayList<String>).add("$i")
        }
        return list
    }

    private fun initNewData(): List<String> {
        newList = ArrayList<String>()
        for (i in 10..60) {
            (newList as ArrayList<String>).add("$i")
        }
        return newList
    }


}


